package com.library.book.service;

import com.framework.Page;
import com.library.book.dao.BookDaoImpl;
import com.library.bookClass.dao.BookClassDaoImpl;
import com.library.entity.Book;
import com.library.entity.BookClass;
import com.library.entity.Staff;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional(readOnly=false)
public class BookServiceImpl {
	
	@Resource
	private BookDaoImpl bookDaoImpl;
	@Resource
	private BookClassDaoImpl bookClassImpl;

	public void addBook(Book book,String path,MultipartFile file){
		this.bookDaoImpl.saveBook(book,path,file);
	}

	/******************查找图书******************/
	/*精确查找*/
	//ID
	@Transactional(readOnly=true)
	public Book getBook(int bookId){
		return this.bookDaoImpl.getBook(bookId);
	}

	//ISBN
	@Transactional(readOnly=true)
	public Book getBookByISBN(String ISBN){
		return this.bookDaoImpl.getBookByISBN(ISBN);
	}


	@Transactional(readOnly=true)
	public Page<Book> getBookPage(int pageNum, int pageSize, Map paramsMap){
		return this.bookDaoImpl.findPageByProperty(pageNum,pageSize,paramsMap);
	}

    @Transactional(readOnly=true)
    public List<Book> getBooks(Map paramsMap){
        return this.bookDaoImpl.findBooksByProperty(paramsMap);
    }


	//多关键字OR查找
	@Transactional(readOnly=true)
	public List<Book> getBooks(String bookName, String author,String publish,BookClass bookClass){
		return this.bookDaoImpl.findByBookInfo(bookName,author,publish,bookClass);
	}

	//多关键字OR查找
	@Transactional(readOnly=true)
	public Page<Book> getBooks(int pageNum,int pageSize,String bookName,String author, BookClass bookClass){
		return this.bookDaoImpl.findByBookInfo(pageNum,pageSize,bookName,author,bookClass);
	}


	//模糊查找
	@Transactional(readOnly=true)
	public List<Book> getBooksVague(String bookName, String author,String publish,BookClass bookClass){
		return this.bookDaoImpl.findVagueByBookInfo(bookName,author,publish,bookClass);
	}

	@Transactional(readOnly=true)
	public Page<Book> getBooksVague(int pageNum,int pageSize,String bookName, String author,String publish,BookClass bookClass){
		return this.bookDaoImpl.findVagueByBookInfo(pageNum,pageSize,bookName,author,publish,bookClass);
	}

	//模糊查找
	@Transactional(readOnly=true)
	public List<Book> getBooksVague(String bookName, String author,String publish){
		return this.bookDaoImpl.findVagueByBookInfo(bookName,author,publish);
	}
	@Transactional(readOnly=true)
	public Page<Book> getBooksVague(int pageNum,int pageSize,String bookName, String author,String publish){
		return this.bookDaoImpl.findVagueByBookInfo(pageNum,pageSize,bookName,author,publish);
	}

	@Transactional(readOnly=true)
	/*查找全部图书 不分页*/
	public List<Book> getAllBooks(){
		return this.bookDaoImpl.findBooksByProperty(new HashMap());
	}
	/*查找全部图书 分页*/
	@Transactional(readOnly=true)
	public Page<Book> getAllBooks(int pageNum,int pageSize){
		return this.bookDaoImpl.findPageByProperty(pageNum,pageSize,new HashMap());
	}

	/*通过员工查找图书 分页*/
	@Transactional(readOnly=true)
	public Page<Book> getBooksByStaff(int pageNum,int pageSize,Staff staff){
		return this.bookDaoImpl.findByStaff(pageNum,pageSize,staff);
	}

	/*通过员工查找图书 不分页*/
	public List<Book> getBooksByStaff(Staff staff){
		return this.bookDaoImpl.findByStaff(staff);
	}

	@Transactional(readOnly=true)
	public Long getBooksCountByParams(Map paramsMap){
		return this.bookDaoImpl.findCountByProperty(paramsMap);
	}

	/*查找全部类别的热门书籍*/
	@Transactional(readOnly=true)
	public List<Book> getAllClassHotBook(int num){
		List<Book> oneClassHotBooks;
		List<Book> allHotBooks = new ArrayList<>();
		List<BookClass> bookClasses = this.bookClassImpl.getAll();
		BookClass bc;
		Map paramsMap = new HashMap();
		Iterator<BookClass> it = bookClasses.iterator();

		while(it.hasNext()){
			bc = it.next();
			paramsMap.put("bookClass",bc);
			oneClassHotBooks = this.bookDaoImpl.findBooksByPropertyAndHot(paramsMap,num);

			//只查询前五个
			for(int i = oneClassHotBooks.size() - 1;i > 4;--i){
				oneClassHotBooks.remove(i);
			}
			allHotBooks.addAll(oneClassHotBooks);
		}
        allHotBooks.sort(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
				if(o1.getBrowseTimes() < o2.getBrowseTimes()){
					return 1;
				}else if(o1.getBrowseTimes() == o2.getBrowseTimes()){
					if(o1.getBorrowTimes() < o2.getBorrowTimes()){
						return 1;
					}else return -1;
				} else
					return -1;
            }
        });
		return allHotBooks;
	}

	/*查找全部图书按照热度排*/
	@Transactional(readOnly=true)
	public List<Book> getBooksByHot(Map paramsMap,int num){
		List<Book> allHotBooks = this.bookDaoImpl.findBooksByPropertyAndHot(paramsMap,num);

		allHotBooks.sort(new Comparator<Book>() {
			@Override
			public int compare(Book o1, Book o2) {
				if(o1.getBrowseTimes() < o2.getBrowseTimes()){
					return 1;
				}else if(o1.getBrowseTimes() == o2.getBrowseTimes()){
					if(o1.getBorrowTimes() < o2.getBorrowTimes()){
						return 1;
					}else return -1;
				} else
					return -1;
			}
		});
		return allHotBooks;
	}

    /*************改删操作***********/
	public void editBook(Book book){
		this.bookDaoImpl.updateBook(book);
	}

	public void editBookImage(Book book,String path,MultipartFile file){
		Book b=this.bookDaoImpl.getBook(book.getBookId());
		this.bookDaoImpl.updateBookImage(b,path,file);
	}

	public void deleteBook(int bookId,String path){
		Book b = this.getBook(bookId);
		this.bookDaoImpl.deleteBook(b,path);
	}

	/*删除多个图书*/
	public void deleteBookGroup(int[] books,String path){
		Book b = null;
		for(int i = 0;i < books.length;++i){
			b = this.getBook(books[i]);
			this.bookDaoImpl.deleteBook(b,path);
		}

	}

}
